﻿using MyX.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LearningEnglish.Models
{
    /// <summary>
    /// 访问类
    /// <para>该类代理实体的访问行为，控制访问权限，访问状态</para>
    /// </summary>
    public class Token : XEntity
    {
        public Token(Guid entityID, TimeSpan liveTime, bool isExclusive = false)
            : base()
        {
            Update(new TokenCreated() { EntityID = entityID, LiveTime = liveTime, IsExclusive = isExclusive });
        }

        public Token(Guid id)
            : base(id)
        {

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public Guid Login(Channel channel)
        {
            var ticket = Guid.NewGuid();
            Update(new TokenLogined() { Channel = channel, TicketValue = ticket });
            return ticket;
        }

        /// <summary>
        /// 注销
        /// </summary>
        public void Logout(Channel channel)
        {
            Update(new TokenLogouted());
        }

        /// <summary>
        /// 改变Token的全局独占性，以及全局票据存活时间
        /// </summary>
        /// <param name="liveTime"></param>
        /// <param name="isExclusive"></param>
        public void Change(TimeSpan liveTime, bool isExclusive)
        {
            Update(new TokenGlobalChanged() { LiveTime = liveTime, IsExclusive = isExclusive });
        }

        /// <summary>
        /// 改变单独渠道中Token的独占性，以及票据存活时间
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="liveTime"></param>
        /// <param name="isExclusive"></param>
        public void Change(Channel channel, TimeSpan liveTime, bool isExclusive)
        {
            Update(new TokenChannelChanged() { Channel = channel, LiveTime = liveTime, IsExclusive = isExclusive });
        }


        public void Do(TokenCreated e)
        {
            Entity = e.EntityID;
            LiveTime = e.LiveTime;
        }

        public void Do(TokenGlobalChanged e)
        {
            LiveTime = e.LiveTime;
            IsExclusive = e.IsExclusive;
        }

        public void Do(TokenChannelChanged e)
        {

        }

        public void Do(TokenLogined e)
        {

        }

        public void Do(TokenLogouted e)
        {

        }

        public Guid Entity { get; set; }

        /// <summary>
        /// 本次操作所来源的渠道
        /// </summary>
        public enum Channel
        {
            APP,
            Web,
            Client,
            Unknow
        }

        /// <summary>
        /// 全局独占性
        /// </summary>
        public bool IsExclusive { get; private set; }

        public TimeSpan LiveTime { get; private set; }

        /// <summary>
        /// 默认设置一小时过时
        /// </summary>
        public static TimeSpan StaticLiveTime = new TimeSpan(1, 0, 0);
    }


    public class TokenCreated : XEvent
    {
        public Guid EntityID { get; set; }
        public TimeSpan LiveTime { get; set; }
        public bool IsExclusive { get; set; }
    }

    public class TokenGlobalChanged : XEvent
    {
        public TimeSpan LiveTime { get; set; }
        public bool IsExclusive { get; set; }
    }

    public class TokenChannelChanged : XEvent
    {
        public Token.Channel Channel { get; set; }
        public TimeSpan LiveTime { get; set; }
        public bool IsExclusive { get; set; }
    }

    public class TokenLogined : XEvent
    {
        public Token.Channel Channel { get; set; }
        public Guid TicketValue { get; set; }
    }

    public class TokenLogouted : XEvent
    {
        public Token.Channel Channel { get; set; }
        public Guid TicketValue { get; set; }
    }
}